package parser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Vector;

import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import beans.Course;
import beans.News;
import beans.WebCampusFile;
import beans.WebCampusFolder;

public class GetMoreInfoClaseParser {

	public void parseXML(Course c, String xml) {

		Vector newsVector = new Vector();
		Vector folders = new Vector();
		int cantCarpeta=0;

		if (c.getNews().isEmpty() || c.getFolders().isEmpty() || c.getNews()==null || c.getFolders()==null){

			try {
				Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
				.parse(new ByteArrayInputStream(xml.getBytes("ISO-8859-1")));
				doc.getDocumentElement().normalize();
				NodeList list = doc.getElementsByTagName("*");
				String node = new String();
				String element = new String();

				News news = null;
				WebCampusFile file = null;
				WebCampusFolder folder = null;

				for (int i = 0; i < list.getLength(); i++) {

					if (list.item(i).hasChildNodes()){
						Node value = list.item(i).getChildNodes().item(0);
						node = list.item(i).getNodeName();
						element = value.getNodeValue();

						System.out.println("******************************************"+node+": "+element);

						// novedad
						if (node.equals("Novedad")){
							Element elem = (Element)list.item(i);
							news = parseNews(elem);
							newsVector.addElement(news);

						} else
							// carpeta
							if(node.equals("Carpeta")){
								if(cantCarpeta!=0){
									folders.addElement(folder);
								}
								System.out.println("******************************///// Carpeta "+cantCarpeta);
								Element elem = (Element) list.item(i);
								folder = parseFolder(elem);
								cantCarpeta++;

							}else
								// file
								if (node.equals("Archivo")){
									System.out.println("******************************///// Archivo ");
									Element elem = (Element) list.item(i);
									file = parseFile(elem);
									folder.addFile(file);
								} 				
					}
				}
				if (cantCarpeta>0){
					folders.addElement(folder);
				}
				//set the parsed folders and news
				c.setFolders(folders);
				c.setNews(newsVector);
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			}
			System.out.println(" --------------- DONE ");
		}
	}
	private WebCampusFile parseFile(Element elem) {
		return new WebCampusFile(ParserMethods.getTextValue(elem,"Nombre"),ParserMethods.getTextValue(elem, "Link"),
				ParserMethods.getTextValue(elem, "Extension"),Long.parseLong(ParserMethods.getTextValue(elem, "Size")),
				ParserMethods.getTextValue(elem, "Fecha"));
	}

	private WebCampusFolder parseFolder(Element elem) {
		return new WebCampusFolder(ParserMethods.getTextValue(elem,"Nombre"));
	}

	private News parseNews(Element elem) {
		return new News(ParserMethods.getTextValue(elem,"Fecha"),ParserMethods.getTextValue(elem, "Titulo"),
				ParserMethods.getTextValue(elem, "Descripcion"));
	}
}
